/*
 * File:   aio.hpp
 * Author: Yuri Babich
 *
 * Created on October 26, 2011, 9:12 PM
 */

#ifndef POSIX_WRAPERS_AIO_HPP_____
#define POSIX_WRAPERS_AIO_HPP_____

#include "config.hpp"
#include <aio.h>
#include <memory.h>
#include <cstddef>

// *****************************************************************************
// * Begin PosixWrap namespace
// *****************************************************************************

BEGIN_PSX_NAMESPACE

/*
 * The class is wrapper of aiocb structure
 */
class AIO_CB : private aiocb
{
    public:

      AIO_CB();

      AIO_CB& setFileDescriptor(int fileDescriptor);
      AIO_CB& setFileOffset(off_t fileOffset);
      AIO_CB& setBuffer(volatile void* buffer);
      AIO_CB& setTransferLength(std::size_t nbytes);
      AIO_CB& setRequestPriorityOffset(int requestPriorityOffset);
      AIO_CB& setLioOpCode(int opCode);

      int getFileDescriptor() const;
      off_t getFileOffset() const;
      volatile void* getBuffer() const;
      size_t getTransferLength() const;
      int getRequestPriorityOffset() const;
      int getLioOpCode() const;

      const aiocb& getInternal() const;
      aiocb& getInternal();

      // TODO sigevent

};

/*
 * Class AIO_CB, inline methods
 */
inline AIO_CB::AIO_CB()
{
    memset(static_cast<aiocb*>(this),0,sizeof(aiocb));
}

inline AIO_CB&  AIO_CB::setFileDescriptor(int fileDescriptor)
{
    this->aio_fildes = fileDescriptor;
    return *this;
}

inline AIO_CB& AIO_CB::setFileOffset(off_t fileOffset)
{
    this->aio_offset = fileOffset;
    return *this;
}

inline AIO_CB& AIO_CB::setBuffer(volatile void* buffer)
{
    this->aio_buf = buffer;
    return *this;
}

inline AIO_CB& AIO_CB::setTransferLength(size_t nbytes)
{
    this->aio_nbytes = nbytes;
    return *this;
}

inline AIO_CB& AIO_CB::setRequestPriorityOffset(int requestPriorityOffset)
{
    this->aio_reqprio = requestPriorityOffset;
    return *this;
}

inline AIO_CB& AIO_CB::setLioOpCode(int opCode)
{
    this->aio_lio_opcode = opCode;
    return *this;
}

inline int AIO_CB::getFileDescriptor() const
{
    return this->aio_fildes;
}

inline off_t AIO_CB::getFileOffset() const
{
    return this->aio_offset;
}

inline volatile void* AIO_CB::getBuffer() const
{
    return this->aio_buf;
}

inline size_t AIO_CB::getTransferLength() const
{
    return this->aio_nbytes;
}

inline int AIO_CB::getRequestPriorityOffset() const
{
    return this->aio_reqprio;
}

inline int AIO_CB::getLioOpCode() const
{
    return this->aio_lio_opcode;
}

inline const aiocb& getInternal() const
{
    return *this;
}

inline aiocb& getInternal()
{
    return *this;
}


// *****************************************************************************
// * End PosixWrap namespace
// *****************************************************************************
END_PSX_NAMESPACE

#endif  /* POSIX_WRAPERS_AIO_HPP_____ */
